﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using LINQtoFxCop.Core;

namespace LINQtoFxCop.DomainSpecific.Examples
{
  internal sealed class XmlConfigurableRule : DomainSpecificIntrospectionRule
  {
    private readonly HashSet<string> _reservedBaseTypes;

    public XmlConfigurableRule()
      : base("XmlConfigurableRule")
    {
      _reservedBaseTypes = new HashSet<string>();
      Init();
    }

    private void Init()
    {
      var configuration = XElement.Load("XmlConfigurableRule.config");

      configuration.Elements("ReservedType")
        .ForEach(element => _reservedBaseTypes.Add(element.Attribute("value").Value));
    }
    protected override IEnumerable<QueryResult> Query()
    {
      return
        from type in Types
        where type.IsSubTypeOf(_reservedBaseTypes)
        select Result(type, type.BaseTypeNames());
    }
  }
}
